<?php
function cleanText ($text,$html=true) {
        $text = preg_replace( "'<script[^>]*>.*?</script>'si", '', $text );
        $text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is', '\2 (\1)', $text );
        $text = preg_replace( '/<!--.+?-->/', '', $text );
        $text = preg_replace( '/{.+?}/', '', $text );
        $text = preg_replace( '/&nbsp;/', ' ', $text );
        $text = preg_replace( '/&amp;/', ' ', $text );
        $text = preg_replace( '/&quot;/', ' ', $text );
        $text = strip_tags( $text );
        $text = preg_replace("/\r\n\r\n\r\n+/", " ", $text);
        $text = $html ? htmlspecialchars( $text ) : $text;
        return $text;
}
function lihatData($query, $variabel){
	$hasil = db_query($query);
	if(empty($hasil)){
		echo db_error();
	}
	$jmlHasil = db_num_rows($hasil);
	//echo $query;
	if(empty($jmlHasil)){
		return '';
	}
	$barisHasil=db_fetch_array($hasil);
	return $barisHasil[$variabel];		
}

function input_data_list($name, $default, $type, $style, $query, $id, $value){
	$hasil = db_query($query);
	if(empty($hasil)){
		echo db_error();
	}
	$num_rows = db_num_rows($hasil);
	
	if($num_rows > 50){
		$type = 2;
	}else{
		$type = 1;
	}
	
	$list_combo = '<option %s value="%s">%s</option>';
	
	$input_list = '<select ' . $style . ' name="' . $name . '">';
	$input_list .= sprintf($list_combo, '', '', '[...]');
	while($baris = db_fetch_array($hasil)){
		$wvalue = $value;
		$selected = '';
		foreach($baris as $key => $v){
			$wvalue = str_replace('%' . $key . '%', $baris[$key], $wvalue);
			if($default == $v){
				$selected = ' selected ';
			}
		}
		$input_list .= sprintf($list_combo, $selected, $baris[$id], $wvalue);
	}
	$input_list .= '</select>';
	
	return $input_list;
}
function buatTanggal($inputTanggal){
	$tempBulan = array('01'=>'Januari','02'=>'Februari','03'=>'Maret','04'=>'April','05'=>'Mei','06'=>'Juni','07'=>'Juli','08'=>'Agustus','09'=>'September','10'=>'Oktober','11'=>'November','12'=>'Desember');
	if(empty($inputTanggal))
		return '';
	list($vTahun,$vBulan,$vTanggal) = explode('-',$inputTanggal);
  	$outputTanggal = $vTanggal.' '.$tempBulan[$vBulan].' '.$vTahun;
	return $outputTanggal;
}
function getIdTahunAjaran($tahunMulai, $tahunSelesai){
	list($thn1,$bln1,$tgl1) = explode('-',$tahunMulai);
	list($thn2,$bln2,$tgl2) = explode('-',$tahunSelesai);
	return $thn1.' / '.$thn2;
}
//$tempname and $actualname come from $_FILES varaibles at the top of this reply
function thumb_jpeg($tempname,$actualname,$destination_path2){                 
		global $destination_path;
		
		//$destination_path = "pics/".$actualname;
		$destination_path = $destination_path2.$actualname;
	
		global $new_width; 
		global $new_height;
		
		//set new sizes as you want them
		$new_width = 80;
		$new_height = 80;
		
	//jpeg output quality
		$quality = 100;
	
		$destimg=imagecreatetruecolor($new_width,$new_height) or die("Error dalam Pembuatan Image"); 
	
		$srcimg=imagecreatefromjpeg($tempname) or die("Kesalahan dalam membuka file asal"); 
	
		imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die("Masalah dalam resize"); 
	
		imagejpeg($destimg,$destination_path,$quality) or die("Masalah dalam menyimpan");

}  
function lihatJumlahAbsensi($id,$id_kelas,$status,$semester){
	if(empty($semester)){
		$query = "SELECT count(status) as jumSakit
				  FROM absensiSiswa ab
				  WHERE ab.id_siswa='$id' and ab.id_kelas='$id_kelas' and status='$status'
				  order by ab.tanggalAbsensi ASC";
	}else{
		$query = "SELECT count(status) as jumSakit
				  FROM absensiSiswa ab
				  WHERE ab.id_siswa='$id' and ab.id_kelas='$id_kelas' and status='$status' and semester='$semester'
				  order by ab.tanggalAbsensi ASC";
	}
	//echo $query;
	$hasilQuery = db_query($query);
	if(empty($hasilQuery)){
		echo db_error();
		return '';
	}else{
		$barisQuery = db_fetch_array($hasilQuery);
		return $barisQuery[jumSakit];
		mysql_free_result($hasilQuery);
	}	
}
function lihatJumlahPelanggaran($id,$id_kelas,$status,$semester){
	if(empty($semester)){
		$query = "SELECT count(status) as jumSakit
				  FROM pelanggaranSiswa ab
				  WHERE ab.id_siswa='$id' and ab.id_kelas='$id_kelas' and status='$status'
				  order by ab.tanggalPelanggaran ASC";
	}else{
		$query = "SELECT count(status) as jumSakit
				  FROM pelanggaranSiswa ab
				  WHERE ab.id_siswa='$id' and ab.id_kelas='$id_kelas' and status='$status' and semester='$semester'
				  order by ab.tanggalPelanggaran ASC";
	}
	//echo $query;
	$hasilQuery = db_query($query);
	if(empty($hasilQuery)){
		echo db_error();
		return '';
	}else{
		$barisQuery = db_fetch_array($hasilQuery);
		return $barisQuery[jumSakit];
		mysql_free_result($hasilQuery);
	}	
}
?>